<style lang="less">
    @import "../../../styles/table.less";
</style>
<template>
    <div class="search">
        <Row>
            <Col>
                <Card title="用户证书管理">
                    <Row @keydown.enter.native="handleSearch">
                        <Form ref="searchForm" :model="searchForm" inline :label-width="70">
                            <Form-item label="学员名称" prop="userName">
                                <Input type="text" v-model="searchForm.userName" placeholder="请输入学员名称" clearable style="width: 200px"/>
                            </Form-item>
                            <Form-item label="身份证号" prop="idCard">
                                <Input type="text" v-model="searchForm.idCard" placeholder="请输入身份证号" clearable style="width: 200px"/>
                            </Form-item>
                            <Form-item style="margin-left:-35px;" class="br">
                                <Button @click="handleSearch" type="primary" icon="ios-search">搜索</Button>
                                <Button @click="handleReset">重置</Button>
                                <a class="drop-down" @click="dropDown">{{dropDownContent}}
                                    <Icon :type="dropDownIcon"></Icon>
                                </a>
                            </Form-item>
                        </Form>
                    </Row>
                    <Row class="operation">
                        <Button @click="importFile" icon="md-add">导入证书</Button>
                        <Button @click="download" icon="md-arrow-round-down">下载模板</Button>
                        <!--<Button @click="importUserRecord" icon="md-add">导入使用记录</Button>
                        <Button @click="downloadUserRecord" icon="md-arrow-round-down">下载使用记录模板</Button>-->
                        <circleLoading v-if="operationLoading"/>
                    </Row>
                    <Row>
                        <Table :loading="loading" border :columns="columns"  size="small"  :data="data" sortable="custom" @on-sort-change="changeSort" @on-selection-change="showSelect" ref="table"></Table>
                    </Row>
                    <Row type="flex" justify="end" class="page">
                        <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize" @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]" size="small" show-total show-elevator show-sizer></Page>
                    </Row>
                </Card>
            </Col>
        </Row>
        <Upload action="/base/common/importCertificateExcel" :on-success="handleSuccess"  v-show="false">
            <Button icon="ios-cloud-upload-outline" ref="upload">Upload files</Button>
        </Upload>
        <Upload action="/base/common/importUserRecord" :on-success="handleUserSuccess"  v-show="false">
            <Button icon="ios-cloud-upload-outline" ref="uploadUserRecord">Upload files</Button>
        </Upload>
        <certificate-drawer ref="certificateDrawer"></certificate-drawer>
    </div>
</template>

<script>
    import circleLoading from "../../my-components/circle-loading.vue";
    import {getUserCertificate, delUserCertificate} from "@/api/bus";
    import certificateDrawer from "./certificate-drawer";
    export default {
        name: "certificate-manage",
        components: {
            certificateDrawer,
            circleLoading
        },
        data() {
            return {
                loading: false, // 表单加载状态
                operationLoading: false,
                drop: false,
                selectCount: 0, // 多选计数
                selectList: [], // 多选数据
                dropDownContent: "展开",
                dropDownIcon: "ios-arrow-down",
                searchForm: {
                    pageNumber: 1, // 当前页数
                    pageSize: 10, // 页面大小
                    sort: "u.createTime", // 默认排序字段
                    order: "desc", // 默认排序方式
                    userName: "",
                    idCard: "",
                },
                selectDate: null, //选择日期绑定modal
                columns: [

                    {
                        type: "index",
                        width: 100,
                        align: "center",
                        fixed: "left"
                    },
                    {
                        title: "学员姓名",
                        align: "center",
                        fixed: "left",
                        width: 120,
                        render: (h, params) => {
                            if (params.row.userName) {
                                return h("div", params.row.userName)
                            } else if(params.row.userName2){
                                return h("div", params.row.userName2)
                            } else {
                                return h("div", "未认领")
                            }
                        }
                    },
                    {
                        title: "证书名称",
                        width: 300,
                        align: "center",
                        key: "certificateName"
                    },
                    {
                        title: "证书编号",
                        width: 200,
                        align: "center",
                        key: "certificateNum",
                    },
                    {
                        title: "证书转换证明",
                        width: 120,
                        align: "center",
                        key: "changeStatus",
                        render: (h, params) => {
                            if (params.row.changeStatus === 0) {
                                return h("div", [
                                    h(
                                        "Tag",
                                        {
                                            props: {
                                                color: "blue"
                                            }
                                        },
                                        "可申请证明"
                                    )
                                ]);
                            } else if (params.row.changeStatus === 1) {
                                return h("div", [
                                    h(
                                        "Tag",
                                        {
                                            props: {
                                                color: "#E6941A"
                                            }
                                        },
                                        "已申请证明"
                                    )
                                ]);
                            } else if (params.row.changeStatus === 2) {
                                return h("div", [
                                    h(
                                        "Tag",
                                        {
                                            props: {
                                                color: "#45c9e6"
                                            }
                                        },
                                        "已获得证明"
                                    )
                                ]);
                            } else if (params.row.changeStatus === 3) {
                                return h("div", [
                                    h(
                                        "Tag",
                                        {
                                            props: {
                                                color: "green"
                                            }
                                        },
                                        "转换成功"
                                    )
                                ]);
                            } else if (params.row.changeStatus === 4) {
                                return h("div", [
                                    h(
                                        "Tag",
                                        {
                                            props: {
                                                color: "red"
                                            }
                                        },
                                        "转换失败"
                                    )
                                ]);
                            } else {
                                return h("div", "--");
                            }
                        }
                    },
                    /*{
                        title: "证明有效期",
                        width: 150,
                        align: "center",
                        key: "validityTime",
                    },*/
                    {
                        title: "颁证机构",
                        width: 300,
                        align: "center",
                        key: "award",
                    },
                    {
                        title: "颁证时间",
                        width: 150,
                        align: "center",
                        key: "awardTime",
                    },
                    {
                        title: "有效期",
                        align: "center",
                        width: 150,
                        key: "validity",
                    },
                    {
                        title: "上传登记时间",
                        align: "center",
                        width: 150,
                        key: "createTime",
                    },
                    {
                        title: "身份证号",
                        width: 200,
                        align: "center",
                        key: "idCard",
                    },
                    {
                        title: "操作",
                        key: "action",
                        width: 150,
                        fixed: "right",
                        align: "center",
                        render: (h, params) => {
                            return h("div", [
                                h(
                                    "Button",
                                    {
                                        props: {
                                            type: "primary",
                                            size: "small"
                                        },
                                        style: {
                                            marginRight: "5px"
                                        },
                                        on: {
                                            click: () => {
                                                this.edit(params.row);
                                            }
                                        }
                                    },
                                    "编辑"
                                ),
                                h(
                                    "Button",
                                    {
                                        props: {
                                            type: "primary",
                                            size: "small"
                                        },
                                        style: {
                                            marginRight: "5px"
                                        },
                                        on: {
                                            click: () => {
                                                this.preview(params.row);
                                            }
                                        }
                                    },
                                    "预览"
                                ),
                                /*h(
                                    "Button",
                                    {
                                        props: {
                                            type: "error",
                                            size: "small"
                                        },
                                        on: {
                                            click: () => {
                                                this.remove(params.row);
                                            }
                                        }
                                    },
                                    "删除"
                                )*/
                            ]);
                        }
                    }],
                data: [], // 表单数据
                total: 0 // 表单数据总数
            }
        },
        methods: {
            init() {
                this.getDataList();
            },
            getDataList() {
                this.loading = true;
                getUserCertificate(this.searchForm).then(res => {
                    this.loading = false;
                    if (res.status === true) {
                        this.data = res.data.rows;
                        this.total = res.data.total;
                    }
                });
            },
            edit(v) {
                this.$refs.certificateDrawer.showDrawer(v);
            },
            handleSuccess(response, file, fileList) {
                if(response.status){
                    this.$Message.success(response.info);
                }else{
                    this.$Message.error(response.info);
                }
            },
            handleUserSuccess(response, file, fileList) {
                if(response.status){
                    this.$Message.success(response.info);
                }else{
                    this.$Message.error(response.info);
                }
            },
            importFile() {
                this.$refs.upload.$el.click()
            },
            download() {
                location.href = "../证书模板.xlsx";
            },
            importUserRecord() {
                this.$refs.uploadUserRecord.$el.click()
            },
            downloadUserRecord() {
                location.href = "../使用记录.xlsx";
            },
            preview(v){
                let url = "base/common/downCertifite?userCertifiId=" + v.id
                window.open(url);
            },
            remove(v) {
                if(v.userId){
                    this.$Message.error("证书已被认领，不能删除！");
                    return;
                } else {
                    this.$Modal.confirm({
                        title: "确认删除",
                        content: "您确认要删除?",
                        onOk: () => {
                            delUserCertificate(v.id).then(res => {
                                if (res.status === true) {
                                    this.$Message.success("删除成功");
                                    this.getDataList();
                                }
                            });
                        }

                    });
                }
            },

            //设置课程推荐状态
            handleSearch() {
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.getDataList();
            },
            handleReset() {
                this.$refs.searchForm.resetFields();
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.selectDate = null;
                this.searchForm.startDate = "";
                this.searchForm.endDate = "";
                // 重新加载数据
                this.getDataList();
            },
            dropDown() {
                if (this.drop) {
                    this.dropDownContent = "展开";
                    this.dropDownIcon = "ios-arrow-down";
                } else {
                    this.dropDownContent = "收起";
                    this.dropDownIcon = "ios-arrow-up";
                }
                this.drop = !this.drop;
            },
            changePage(v) {
                this.searchForm.pageNumber = v;
                this.getDataList();
                this.clearSelectAll();
            },
            changePageSize(v) {
                this.searchForm.pageSize = v;
                this.getDataList();
            },
            changeSort(e) {
                this.searchForm.sort = e.key;
                this.searchForm.order = e.order;
                if (e.order === "normal") {
                    this.searchForm.order = "";
                }
                this.getDataList();
            },
            showSelect(e) {
                this.selectList = e;
                this.selectCount = e.length;
            },
            clearSelectAll() {
                this.$refs.table.selectAll(false);
            },
        },
        mounted() {
            this.init();
        }
    }
</script>
